#include "NBiot.h"
#include "NBstring.h"


int suhuix = 0;


/*
*在buf里面查找string,如果找到了就返回string的第一个字符在buf中的位置
*/
int Inquire_String(unsigned char strsize, unsigned char bufsize,char *string , char *buf){
	int i, j, flag;
	for(i = 0; (i < bufsize)&(buf[i] != 0x00) ; i++){
		flag = 0;
		for(j = 0; j < strsize ; j++){
			if(string[j] == buf[i+j]){
			flag++;
			}
		}
		if(flag == strsize){
		return i;
		}
	}
	return -1;				//没有找到
}

/*
*在buf中查找逗号，如果找到了就返回逗号在buf中的位置
*/
int Inquire_Comma(unsigned char bufsize, char *buf, char order){
	int i, flag = 0;
	char p = ',';
	for(i = 0; (i < bufsize)&&(buf[i] != 0x00) ; i++){
		if(p == buf[i]){
			flag++;
			if(flag == order){
				return i;
			}
		}
	}
	return -1;				//±íÊ¾Ã»ÓÐÕÒµ½
}

/*
*在buf中查找冒号，如果找到了就返回冒号在buf中的位置
*/
int Inquire_Colon(unsigned char bufsize, char *buf, char order){
	int i, flag = 0;
	char p = ':';
	for(i = 0; (i < bufsize)&&(buf[i] != 0x00) ; i++){
		if(p == buf[i]){
			flag++;
			if(flag == order){
				return i;
			}
		}
	}
	return -1;				//±íÊ¾Ã»ÓÐÕÒµ½
}


/*
*在buf中的string字符串之后查找第几个逗号
*/
int Inquire_String_comma(unsigned char strsize, unsigned char bufsize,char *string, char *buf, char order){
	int i = 0, j = 0, m = 0, k = 0;
	j = Inquire_String(strsize, bufsize, string, &buf[0]);
	if(j == -1){
		return -1;
	}
	m = Inquire_Comma(bufsize, &buf[j], order);
	if(m == -1){
		return -1;
	}
	return (m + j);
}

/*
*在buf的string字符串之后查找第几个冒号
*/
int Inquire_String_colon(unsigned char strsize, unsigned char bufsize,char *string, char *buf, char order){
	int i = 0, j = 0, m = 0, k = 0;
	j = Inquire_String(strsize, bufsize, string, &buf[0]);
	if(j == -1){
		return -1;
	}
	m = Inquire_Colon(bufsize, &buf[j], order);
	if(m == -1){
		return -1;
	}
	return (m + j);
}


/*
*提取buf的string字符串之后的第n个逗号之后的数据（数据位于两个逗号之间）
*/
void Inquire_String_comma_data(unsigned char strsize, unsigned char bufsize,char *string, char *buf, char order, int * ID){
	int order_1, order_2, i;
	int num = 0;
	order_1 = Inquire_String_comma(strsize, bufsize, string, buf, order);
//	order_2 = Inquire_String_comma(strsize, bufsize, string, buf, order + 1);
	if(-1 == order_1){
		*ID = 0;
		return;
	}
	for(i = order_1 + 1; ; i++){
		if((buf[i] <= 0x39) && (buf[i] >= 0x30)){
			num = num * 10;
			num = num + buf[i] - 0x30;
		}else{
			break;
		}
	}
	*ID = num;
}


/*
*提取buf的string字符串之后的第n个逗号之后的数据（数据位于两个逗号之间）
*/
void Inquire_String_comma_data_hex(unsigned char strsize, unsigned char bufsize,char *string, char *buf, char order, int * ID){
	int order_1, order_2, i;
	int num = 0;
	order_1 = Inquire_String_comma(strsize, bufsize, string, buf, order);
	if(-1 == order_1){
		*ID = 0;
		return;
	}
	for(i = order_1 + 1; ; i++){
		if((buf[i] <= 0x39) && (buf[i] >= 0x30)){
			num = num * 16;
			num = num + buf[i] - 0x30;
		}else if((buf[i] <= 'Z') && (buf[i] >= 'A')){
			num = num * 16;
			num = num + buf[i] - 'A' + 10;
		}else{
			break;
		}
	}
	*ID = num;
}


/*
*提取buf的string字符串之后的第n个逗号之后的 字符串 数据（数据位于两个逗号之间）
*/
void Inquire_String_comma_string(unsigned char strsize, unsigned char bufsize,char *string, char *buf, char order, int * ID){
	int order_1, order_2, i;
	int num = 0;
	order_1 = Inquire_String_comma(strsize, bufsize, string, buf, order);
	order_2 = Inquire_String_comma(strsize, bufsize, string, buf, order + 1);
	if(-1 == order_1){
		*ID = 0;
		return;
	}
	for(i = order_1 + 1; i < order_2; i++){
		if((buf[i] <= 0x39) && (buf[i] >= 0x30)){
			num = num * 10;
			num = num + buf[i] - 0x30;
		}
	}
	*ID = num;
	/*？？？？？？？？？？？？？？？？？？？？？？？？？？？？？*/
}


/*
*提取buf的string字符串之后的第n个冒号之后的数据（数据位于两个冒号之间）
*/
void Inquire_String_colon_data(unsigned char strsize, unsigned char bufsize,char *string, char *buf, char order, char *str){
	int order_1, order_2, i;
	int num = 0;
	order_1 = Inquire_String_colon(strsize, bufsize, string, buf, order);
	order_2 = Inquire_String_comma(strsize, bufsize, string, buf, order);
	for(i = order_1 + 1; i < order_2; i++){
		if((buf[i] <= 0x39) && (buf[i] >= 0x30)){
			num = num * 10;
			num = num + buf[i] - 0x30;
		}
		
	}
	sprintf(str,"%ld",num);
}



/*
*提取buf的string字符串之后的第n个冒号之后的数据（数据之后没有冒号了）
*/
void Inquire_String_colon_data_afternull(unsigned char strsize, unsigned char bufsize,char *string, char *buf, char order, int * ID){
		int order_1, i;
		int num = 0;
		order_1 = Inquire_String_colon(strsize, bufsize, string, buf, order);
		for(i = order_1 + 1; ; i++){
				if((buf[i] <= 0x39) && (buf[i] >= 0x30)){
						num = num * 10;
						num = num + buf[i] - 0x30;
				}else{
					break;
				}
		}
		*ID = num;
}


void Inquire_String_colon_longdata_afternull(unsigned char strsize, unsigned char bufsize,char *string, char *buf, char order, char * ID){
		int order_1, i, j = 0;
		long long num = 0;
		order_1 = Inquire_String_colon(strsize, bufsize, string, buf, order);
		memcpy(ID, buf + order_1 + 1, 15);
}

void Inquire_String_longdata_afternull(unsigned char strsize, unsigned char bufsize,char *string, char *buf, char order, char * ID){
		int order_1, i, j = 0;
		long long num = 0;
//		order_1 = Inquire_String_colon(strsize, bufsize, string, buf, order);
		order_1	= Inquire_String(strsize, bufsize, string, &buf[0]);
		memcpy(ID, buf + order_1 + 7, 15);
}
#ifdef crc_calc
short clac_crc(char *buf, int size){
	return 1;
}

int clac_crc_send(char *buf, int size){
	nb_data_5.crc = clac_crc(buf, size);
	return 1;
}

int clac_crc_receive(char *buf, int size){

	return 1;
}

#endif
int LH_count = 0;
void LH_memcpy(char *dest, char *src, int count){
	int index = 0;
	while(count--){
		dest[LH_count + index] = src[index];
		index++;
	}
}
// sprintf(USART_send2, "AT+MIPLNOTIFY=0,%ld,3200,0,5501,1,5,\"",Observe_MsgID);	
// sprintf(USART_send2, "  AT+MIPLNOTIFY=0,   %ld   ,3200,0,5501,1,5,    \"0123456789\"     ,0,0          ",Observe_MsgID);	
int LH_send_string_Notify(char *nb_data_4, int nb_data_4_size){
	// char Notify_head1[] = "AT+MIPLNOTIFY=0,";
	// char Notify_head2[] = ",3200,0,5501,1,5,";
	char Notify_head3[] = ",0,0";
	int count;
	nb_data_2.frame_serial_number++;
	LH_count = 0;
	memset(USART_send2, 0x00, sizeof(USART_send2));

	/*notify头信息 */
	// LH_memcpy(USART_send2, Notify_head1, sizeof(Notify_head1) - 1);			//
	// LH_memcpy(USART_send2, Observe_MsgID, sizeof(Observe_MsgID) - 1);
	// LH_memcpy(USART_send2, Notify_head2, sizeof(Notify_head2) - 1);
	sprintf(USART_send2, "AT+MIPLNOTIFY=0,%d,3200,0,5501,1,5,\"",Observe_MsgID);
	LH_count = strlen(USART_send2);
	count = LH_count;
	/*notify 的数据*/
	LH_memcpy(USART_send2, (char*)&nb_data_1, sizeof(nb_data_1));
	LH_memcpy(USART_send2, (char*)&nb_data_2, sizeof(nb_data_2));
	LH_memcpy(USART_send2, (char*)&nb_data_3, sizeof(nb_data_3));
	LH_memcpy(USART_send2, (char*)&nb_data_4, nb_data_4_size);
	// clac_crc_send(void *0 , 10);
	LH_memcpy(USART_send2, (char*)&nb_data_5, sizeof(nb_data_5));

	/*notify的尾节点*/
	LH_memcpy(USART_send2, Notify_head3, sizeof(Notify_head3) - 1);

	ONENET_send(USART_send2, LH_count);
}


int LH_send_string_up(char *nb_data_4, int nb_data_4_size){
	PFC++;
	nb_data_3.seq |= (PFC & 0x0f);
	LH_send_string_Notify(nb_data_4, nb_data_4_size);
}

int LH_send_string_ack(char *nb_data_4, int nb_data_4_size){
	nb_data_3.seq |= (RSEQ & 0x0f);
	LH_send_string_Notify(nb_data_4, nb_data_4_size);
}

























